package com.xinbochuang.machine.admin.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinbochuang.machine.admin.domain.*;
import com.xinbochuang.machine.admin.domain.macRoom.MacRoom;
import com.xinbochuang.machine.admin.domain.system.SysDept;
import com.xinbochuang.machine.admin.domain.system.SysUser;
import com.xinbochuang.machine.admin.dto.MacInOutDto;
import com.xinbochuang.machine.admin.mapper.MacinoutAssociationMapper;
import com.xinbochuang.machine.admin.mapper.MacinoutMapper;
import com.xinbochuang.machine.admin.mapper.SysMessageMapper;
import com.xinbochuang.machine.admin.mapper.WorkerMapper;
import com.xinbochuang.machine.admin.mapper.macRoom.MacRoomMapper;
import com.xinbochuang.machine.admin.mapper.system.SysDeptMapper;
import com.xinbochuang.machine.admin.mapper.system.SysUserMapper;
import com.xinbochuang.machine.admin.service.IMacCheckRecordService;
import com.xinbochuang.machine.admin.service.IMacinoutService;
import com.xinbochuang.machine.admin.service.system.ISysDeptService;
import com.xinbochuang.machine.common.domain.LoginUser;
import com.xinbochuang.machine.common.utils.HttpUtils;
import com.xinbochuang.machine.common.utils.SecurityUtils;
import com.xinbochuang.machine.common.utils.ServletUtils;
import com.xinbochuang.machine.common.utils.StringUtils;
import com.xinbochuang.machine.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 杜文博
 * @date 2020-10-14
 */
@Service
public class MacinoutServiceImpl extends ServiceImpl<MacinoutMapper, Macinout> implements IMacinoutService {

    @Resource
    MacinoutMapper macinoutMapper;

    @Resource
    WorkerMapper workerMapper;

    @Resource
    TokenService tokenService;

    @Resource
    SysUserMapper sysUserMapper;

    @Resource
    SysDeptMapper sysDeptMapper;

    @Resource
    MacinoutAssociationMapper macinoutAssociationMapper;

    @Resource
    MacRoomMapper macRoomMapper;
    @Resource
    SysMessageMapper sysMessageMapper;

    @Resource
    private ISysDeptService deptService;

    @Resource
    IMacCheckRecordService iMacCheckRecordService;

    @Value("${websocket.path}")
    String webSocketPath;


    @Override
    public IPage<Macinout> findMacRoomByNameForPage(Macinout macinout) {
        SysUser sysUser = sysUserMapper.selectById(SecurityUtils.getLoginUser().getUser().getUserId());
        macinout.setCreateOrderUserName(sysUser.getNickname());
        QueryWrapper<Macinout> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Macinout::getDeleteFlag, 0).orderByDesc(Macinout::getUpdateTime);
        if (macinout != null) {
            if (StrUtil.isNotBlank(macinout.getOrderName())) {
                queryWrapper.lambda().like(Macinout::getOrderName, macinout.getOrderName());
            }
            if (StrUtil.isNotBlank(macinout.getCreateOrderDeptId())) {
                queryWrapper.lambda().eq(Macinout::getCreateOrderDeptId, macinout.getCreateOrderDeptId());
            }
            if (StrUtil.isNotBlank(macinout.getQueryOrderStatus())) {
                queryWrapper.lambda().inSql(Macinout::getOrderStatus, macinout.getQueryOrderStatus());
                if(macinout.getQueryOrderStatus().contains("0")||macinout.getQueryOrderStatus().contains("3")){
                    queryWrapper.lambda().eq(Macinout::getCreateOrderUserName, macinout.getCreateOrderUserName());
                }
            }
            if (macinout.getWorkType() != null) {
                queryWrapper.lambda().eq(Macinout::getWorkType, macinout.getWorkType());
            }
        }
        return page(new Page<>(macinout.getPageNum(), macinout.getPageSize()), queryWrapper);
    }

    @Override
    public IPage<Macinout> getCheckMacInoutList(Macinout macinout) {
        int userId;
        //此处loginUserId为APP传值，否则就为web
        if (StringUtils.isNotBlank(macinout.getLoginUserId())) {
            userId = Integer.valueOf(macinout.getLoginUserId());
        } else {
            userId = SecurityUtils.getLoginUser().getUser().getUserId();
        }
        System.out.println("--------------getCheckMacInoutList--------"+userId);
        //userId = SecurityUtils.getLoginUser().getUser().getUserId();
        String orderId = macinout.getOrderId();
        String orderTheme = macinout.getOrderTheme();
        Integer orderStatus = macinout.getOrderStatus();


        return macinoutMapper.getMacinoutForCheck(new Page<>(macinout.getPageNum(), macinout.getPageSize()), userId, orderId, orderTheme, orderStatus, macinout.getWorkType(),macinout.getQueryWorkType(),macinout.getQueryOrderStatus());
    }

    @Override
    public Integer getMacinoutForCheckCount(Macinout macinout) {
        Integer userId = SecurityUtils.getLoginUser().getUser().getUserId();
        return macinoutMapper.getMacinoutForCheckCount(userId, macinout.getPageNum(), macinout.getPageSize());
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertMacinout(MacInOutDto macInOutDto) {

        List<Worker> workerList = macInOutDto.getWorker();
        List<MacinoutAssociation> macList1 = macInOutDto.getMacList();
        Map<String, List<MacinoutAssociation>> macMap = macList1.stream().collect(Collectors.groupingBy(t -> t.getMacprincipalId()));
        System.out.println("按机房负责人分组"+macMap);
        /*然后再对map处理，这样就方便取出自己要的数据*/
        Boolean result =false;
        //根据状态判断，如果是暂存就不分工单，如果是提交就分工单
        String  orderStatus = macInOutDto.getOrderStatus()+"";
        if(StringUtils.isNotBlank(orderStatus)&&orderStatus.equals("1")){
                Macinout macinout = new Macinout();
                String macInoutuuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
                Worker worker = new Worker();
                MacinoutAssociation macinoutAssociation = new MacinoutAssociation();
                //插入割接工单号
                if (macInOutDto.getWorkType() == 1) {
                    Calendar cal = Calendar.getInstance();
                    Date date = cal.getTime();
                    String orderNo = "NXGJ"+"-"+ new SimpleDateFormat("yyyyMMddHHmmss").format(date);
                    macinout.setOrderNo(orderNo);
                }
                //插入施工工单号
                if (macInOutDto.getWorkType() == 2) {
                    Calendar cal = Calendar.getInstance();
                    Date date = cal.getTime();
                    String orderNo = "NXSG"+"-" + new SimpleDateFormat("yyyyMMddHHmmss").format(date);
                    macinout.setOrderNo(orderNo);
                }
                //插入安检工单号
                if (macInOutDto.getWorkType() == 3) {
                    Calendar cal = Calendar.getInstance();
                    Date date = cal.getTime();
                    String orderNo = "NXAJ"+"-" + new SimpleDateFormat("yyyyMMddHHmmss").format(date);
                    macinout.setOrderNo(orderNo);
                }
                //插入其它类型工单号
                if (macInOutDto.getWorkType() == 4) {
                    Calendar cal = Calendar.getInstance();
                    Date date = cal.getTime();
                    String orderNo = "NXQT"+"-" + new SimpleDateFormat("yyyyMMddHHmmss").format(date);
                    macinout.setOrderNo(orderNo);
                }
                macinout.setUuid(macInoutuuid);
                macinout.setDeleteFlag(0);
                macinout.setOrderStatus(macInOutDto.getOrderStatus());

                macinout.setAlarm(macInOutDto.getAlarm());
                macinout.setCreateTime(DateUtil.now());
                macinout.setUpdateTime(DateUtil.now());
                macinout.setMacPrincipal(macInOutDto.getMacPrincipal());
                macinout.setOrderId(macInOutDto.getOrderId());
                macinout.setOrderThirdName(macInOutDto.getOrderThirdName());
                macinout.setCabinetName(macInOutDto.getCabinetName());
                macinout.setCabinetId(macInOutDto.getCabinetId());
                macinout.setCity(macInOutDto.getCity());
                macinout.setConstructionDept(macInOutDto.getConstructionDept());
                macinout.setConstructionDeptId(macInOutDto.getConstructionDeptId());
                macinout.setCreateOrderUserId(macInOutDto.getCreateOrderUserId());
                //		macinout.setCreateOrderUserName(SecurityUtils.getUsername());
                macinout.setMacName(macInOutDto.getMacName());
                macinout.setMacId(macInOutDto.getMacId());
                macinout.setOrderName(macInOutDto.getOrderName());
                macinout.setOrderTheme(macInOutDto.getOrderTheme());
                macinout.setWorkBeginTime(macInOutDto.getWorkBeginTime());
                macinout.setWorkEndTime(macInOutDto.getWorkEndTime());
                macinout.setOtherWork(macInOutDto.getOtherWork());
                macinout.setWorkType(macInOutDto.getWorkType());
                macinout.setFreeWorkerName(macInOutDto.getFreeWorkerName());
                macinout.setWorkIngPrincpcipal(macInOutDto.getWorkIngPrincpcipal());
                macinout.setWorkIngPrincpcipalTel(macInOutDto.getWorkIngPrincpcipalTel());
                macinout.setWorkIngPrincpcipalDept(macInOutDto.getWorkIngPrincpcipalDept());
                macinout.setWorkIngPrincpcipalDeptId(macInOutDto.getWorkIngPrincpcipalDeptId());
                macinout.setWorkIngPrincpcipalId(macInOutDto.getWorkIngPrincpcipalId());
                macinout.setFreeWorker(macInOutDto.getFreeWorker());
                macinout.setFreeDept(macInOutDto.getFreeDept());
                macinout.setFreeDeptId(macInOutDto.getFreeDeptId());
                macinout.setCreateOrderUserName(macInOutDto.getCreateOrderUserName());
                macinout.setCreateOrderDeptId(macInOutDto.getCreateOrderDeptId());
                SysDept dept =deptService.selectDeptById(Integer.valueOf(macInOutDto.getCreateOrderDeptId()));
                //macinout.setCreateOrderDept(macInOutDto.getCreateOrderDept());
                macinout.setCreateOrderDept(dept.getDeptName());
                result = save(macinout);

                //记录工单审核状态
                iMacCheckRecordService.insertCheckRecord(macInOutDto.getLoginUserId()+"",macinout.getOrderStatus()+"",macInoutuuid);

                if (workerList.size() != 0) {
                    for (int i = 0; i < workerList.size(); i++) {
                        String workId = UUID.randomUUID().toString().trim().replaceAll("-", "");
                        worker.setUuid(workId);
                        worker.setMacInoutId(macInoutuuid);
                        worker.setConstructionDept(workerList.get(i).getConstructionDept());
                        worker.setConstructionDeptId(workerList.get(i).getConstructionDeptId());
                        worker.setWorkTel(workerList.get(i).getWorkTel());
                        worker.setWorkIdCard(workerList.get(i).getWorkIdCard());
                        worker.setDeleteFlag(0);
                        worker.setWorkerId(workerList.get(i).getWorkerId());
                        worker.setWorkName(workerList.get(i).getWorkName());
                        workerMapper.insert(worker);
                    }
                }
                if (macList1.size() != 0) {
                    for (int i = 0; i < macList1.size(); i++) {
                        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
                        macinoutAssociation.setUuid(uuid);
                        macinoutAssociation.setMacId(macList1.get(i).getMacId());
                        macinoutAssociation.setDeleteFlag(0);
                        macinoutAssociation.setMacinoutId(macInoutuuid);
                        macinoutAssociation.setMacName(macList1.get(i).getMacName());
                        macinoutAssociation.setMacprincipal(macList1.get(i).getMacprincipal());
                        macinoutAssociation.setMacprincipalId(macList1.get(i).getMacprincipalId());
                        macinoutAssociation.setMacprincipalTel(macList1.get(i).getMacprincipalTel());
                        macinoutAssociation.setUpdateTime(DateUtil.now());
                        macinoutAssociation.setCreateTime(DateUtil.now());
                        macinoutAssociation.setCreateBy(macInOutDto.getCreateOrderUserName());
                        macinoutAssociation.setUpdateBy(macInOutDto.getCreateOrderUserName());
                        macinoutAssociation.setOrderStatus(macInOutDto.getOrderStatus());
                        macinoutAssociationMapper.insert(macinoutAssociation);

                        SysUser sysUser = sysUserMapper.selectUserById(Integer.valueOf(macinoutAssociation.getMacprincipalId()));
                        SysMessage sysMessage = new SysMessage();
                        sysMessage.setId(UUID.randomUUID().toString());
                        sysMessage.setStatus("0");
                        sysMessage.setCreateTime(new Date());
                        sysMessage.setType("2");
                        sysMessage.setUserId(sysUser.getIdCard());
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("id", macinout.getUuid());
                        jsonObject.put("type", macinout.getWorkType());
                        sysMessage.setContent(JSONObject.toJSONString(jsonObject));
                        sysMessageMapper.insert(sysMessage);
                        JSONObject jsonObject1 = new JSONObject();
                        jsonObject1.put("vmc", sysUser.getIdCard());
                        jsonObject1.put("message", JSONObject.toJSONString(sysMessage));
                        HttpUtils.post(webSocketPath, JSONObject.toJSONString(jsonObject1));
                    }
                }
        }else {
            for(Map.Entry<String, List<MacinoutAssociation>> entry : macMap.entrySet()){
                //获取分组后的机房信息
                List<MacinoutAssociation> macList =entry.getValue();

                Macinout macinout = new Macinout();
                String macInoutuuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
                Worker worker = new Worker();
                MacinoutAssociation macinoutAssociation = new MacinoutAssociation();
                //插入割接工单号
                if (macInOutDto.getWorkType() == 1) {
                    Calendar cal = Calendar.getInstance();
                    Date date = cal.getTime();
                    String orderNo = "NXGJ"+"-"+ new SimpleDateFormat("yyyyMMddHHmmss").format(date);
                    macinout.setOrderNo(orderNo);
                }
                //插入施工工单号
                if (macInOutDto.getWorkType() == 2) {
                    Calendar cal = Calendar.getInstance();
                    Date date = cal.getTime();
                    String orderNo = "NXSG"+"-" + new SimpleDateFormat("yyyyMMddHHmmss").format(date);
                    macinout.setOrderNo(orderNo);
                }
                //插入安检工单号
                if (macInOutDto.getWorkType() == 3) {
                    Calendar cal = Calendar.getInstance();
                    Date date = cal.getTime();
                    String orderNo = "NXAJ"+"-" + new SimpleDateFormat("yyyyMMddHHmmss").format(date);
                    macinout.setOrderNo(orderNo);
                }
                //插入其它类型工单号
                if (macInOutDto.getWorkType() == 4) {
                    Calendar cal = Calendar.getInstance();
                    Date date = cal.getTime();
                    String orderNo = "NXQT"+"-" + new SimpleDateFormat("yyyyMMddHHmmss").format(date);
                    macinout.setOrderNo(orderNo);
                }
                macinout.setUuid(macInoutuuid);
                macinout.setDeleteFlag(0);
                macinout.setOrderStatus(macInOutDto.getOrderStatus());

                macinout.setAlarm(macInOutDto.getAlarm());
                macinout.setCreateTime(DateUtil.now());
                macinout.setUpdateTime(DateUtil.now());
                macinout.setMacPrincipal(macInOutDto.getMacPrincipal());
                macinout.setOrderId(macInOutDto.getOrderId());
                macinout.setOrderThirdName(macInOutDto.getOrderThirdName());
                macinout.setCabinetName(macInOutDto.getCabinetName());
                macinout.setCabinetId(macInOutDto.getCabinetId());
                macinout.setCity(macInOutDto.getCity());
                macinout.setConstructionDept(macInOutDto.getConstructionDept());
                macinout.setConstructionDeptId(macInOutDto.getConstructionDeptId());
                macinout.setCreateOrderUserId(macInOutDto.getCreateOrderUserId());
                //		macinout.setCreateOrderUserName(SecurityUtils.getUsername());
                macinout.setMacName(macInOutDto.getMacName());
                macinout.setMacId(macInOutDto.getMacId());
                macinout.setOrderName(macInOutDto.getOrderName());
                macinout.setOrderTheme(macInOutDto.getOrderTheme());
                macinout.setWorkBeginTime(macInOutDto.getWorkBeginTime());
                macinout.setWorkEndTime(macInOutDto.getWorkEndTime());
                macinout.setOtherWork(macInOutDto.getOtherWork());
                macinout.setWorkType(macInOutDto.getWorkType());
                macinout.setFreeWorkerName(macInOutDto.getFreeWorkerName());
                macinout.setWorkIngPrincpcipal(macInOutDto.getWorkIngPrincpcipal());
                macinout.setWorkIngPrincpcipalTel(macInOutDto.getWorkIngPrincpcipalTel());
                macinout.setWorkIngPrincpcipalDept(macInOutDto.getWorkIngPrincpcipalDept());
                macinout.setWorkIngPrincpcipalDeptId(macInOutDto.getWorkIngPrincpcipalDeptId());
                macinout.setWorkIngPrincpcipalId(macInOutDto.getWorkIngPrincpcipalId());
                macinout.setFreeWorker(macInOutDto.getFreeWorker());
                macinout.setFreeDept(macInOutDto.getFreeDept());
                macinout.setFreeDeptId(macInOutDto.getFreeDeptId());
                macinout.setCreateOrderUserName(macInOutDto.getCreateOrderUserName());
                macinout.setCreateOrderDeptId(macInOutDto.getCreateOrderDeptId());
                SysDept dept =deptService.selectDeptById(Integer.valueOf(macInOutDto.getCreateOrderDeptId()));
                //macinout.setCreateOrderDept(macInOutDto.getCreateOrderDept());
                macinout.setCreateOrderDept(dept.getDeptName());
                result = save(macinout);

                //记录工单审核状态
                iMacCheckRecordService.insertCheckRecord(macInOutDto.getLoginUserId()+"",macinout.getOrderStatus()+"",macInoutuuid);

                if (workerList.size() != 0) {
                    for (int i = 0; i < workerList.size(); i++) {
                        String workId = UUID.randomUUID().toString().trim().replaceAll("-", "");
                        worker.setUuid(workId);
                        worker.setMacInoutId(macInoutuuid);
                        worker.setConstructionDept(workerList.get(i).getConstructionDept());
                        worker.setConstructionDeptId(workerList.get(i).getConstructionDeptId());
                        worker.setWorkTel(workerList.get(i).getWorkTel());
                        worker.setWorkIdCard(workerList.get(i).getWorkIdCard());
                        worker.setDeleteFlag(0);
                        worker.setWorkerId(workerList.get(i).getWorkerId());
                        worker.setWorkName(workerList.get(i).getWorkName());
                        workerMapper.insert(worker);
                    }
                }
                if (macList.size() != 0) {
                    for (int i = 0; i < macList.size(); i++) {
                        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
                        macinoutAssociation.setUuid(uuid);
                        macinoutAssociation.setMacId(macList.get(i).getMacId());
                        macinoutAssociation.setDeleteFlag(0);
                        macinoutAssociation.setMacinoutId(macInoutuuid);
                        macinoutAssociation.setMacName(macList.get(i).getMacName());
                        macinoutAssociation.setMacprincipal(macList.get(i).getMacprincipal());
                        macinoutAssociation.setMacprincipalId(macList.get(i).getMacprincipalId());
                        macinoutAssociation.setMacprincipalTel(macList.get(i).getMacprincipalTel());
                        macinoutAssociation.setUpdateTime(DateUtil.now());
                        macinoutAssociation.setCreateTime(DateUtil.now());
                        macinoutAssociation.setCreateBy(macInOutDto.getCreateOrderUserName());
                        macinoutAssociation.setUpdateBy(macInOutDto.getCreateOrderUserName());
                        macinoutAssociation.setOrderStatus(macInOutDto.getOrderStatus());
                        macinoutAssociationMapper.insert(macinoutAssociation);

                        SysUser sysUser = sysUserMapper.selectUserById(Integer.valueOf(macinoutAssociation.getMacprincipalId()));
                        SysMessage sysMessage = new SysMessage();
                        sysMessage.setId(UUID.randomUUID().toString());
                        sysMessage.setStatus("0");
                        sysMessage.setCreateTime(new Date());
                        sysMessage.setType("2");
                        sysMessage.setUserId(sysUser.getIdCard());
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("id", macinout.getUuid());
                        jsonObject.put("type", macinout.getWorkType());
                        sysMessage.setContent(JSONObject.toJSONString(jsonObject));
                        sysMessageMapper.insert(sysMessage);
                        JSONObject jsonObject1 = new JSONObject();
                        jsonObject1.put("vmc", sysUser.getIdCard());
                        jsonObject1.put("message", JSONObject.toJSONString(sysMessage));
                        HttpUtils.post(webSocketPath, JSONObject.toJSONString(jsonObject1));
                    }
                }
            }
        }

        return result;
    }

    @Override
    public boolean updateMacinout(MacInOutDto macInOutDto) {
        String loginUserId=macInOutDto.getLoginUserId();
        boolean result = false;
                //根据状态判断，如果是暂存就不分工单，如果是提交就分工单
        String  orderStatus = macInOutDto.getOrderStatus()+"";
        if(StringUtils.isNotBlank(orderStatus)&&orderStatus.equals("1")){
            //记录工单审核状态
            iMacCheckRecordService.insertCheckRecord(macInOutDto.getLoginUserId()+"",macInOutDto.getOrderStatus()+"",macInOutDto.getUuid());
            String userName="";
            if(StringUtils.isNotBlank(loginUserId)){
                userName=sysUserMapper.selectUserById(Integer.valueOf(macInOutDto.getLoginUserId())).getNickname();
            }else{
                userName=SecurityUtils.getLoginUser().getUser().getNickname();
            }
            Macinout macinout = new Macinout();
            macinout.setUuid(macInOutDto.getUuid());
            macinout.setOrderStatus(macInOutDto.getOrderStatus());
            macinout.setCreateTime(DateUtil.now());
            macinout.setUpdateTime(DateUtil.now());
            macinout.setAlarm(macInOutDto.getAlarm());
            macinout.setImgUrl(macInOutDto.getImgUrl());
            macinout.setMacPrincipal(macInOutDto.getMacPrincipal());
            macinout.setCabinetName(macInOutDto.getCabinetName());
            macinout.setCabinetId(macInOutDto.getCabinetId());
            macinout.setCity(macInOutDto.getCity());
            macinout.setConstructionDept(macInOutDto.getConstructionDept());
            macinout.setConstructionDeptId(macInOutDto.getConstructionDeptId());
            macinout.setCreateOrderUserId(macInOutDto.getCreateOrderUserId());
            macinout.setOrderId(macInOutDto.getOrderId());
            macinout.setOrderThirdName(macInOutDto.getOrderThirdName());
            macinout.setCheckDetail("");
//		macinout.setCreateOrderUserName(SecurityUtils.getUsername());
            macinout.setMacName(macInOutDto.getMacName());
            macinout.setMacId(macInOutDto.getMacId());
            macinout.setOrderName(macInOutDto.getOrderName());
            macinout.setOrderTheme(macInOutDto.getOrderTheme());
            macinout.setWorkBeginTime(macInOutDto.getWorkBeginTime());
            macinout.setWorkEndTime(macInOutDto.getWorkEndTime());
            macinout.setOtherWork(macInOutDto.getOtherWork());
            macinout.setWorkType(macInOutDto.getWorkType());
            macinout.setWorkIngPrincpcipal(macInOutDto.getWorkIngPrincpcipal());
            macinout.setWorkIngPrincpcipalTel(macInOutDto.getWorkIngPrincpcipalTel());
            macinout.setWorkIngPrincpcipalId(macInOutDto.getWorkIngPrincpcipalId());
            macinout.setFreeWorker(macInOutDto.getFreeWorker());
            macinout.setFreeDept(macInOutDto.getFreeDept());
            macinout.setFreeDeptId(macInOutDto.getFreeDeptId());
            macinout.setCreateOrderUserName(macInOutDto.getCreateOrderUserName());
            macinout.setCreateOrderDept(macInOutDto.getCreateOrderDept());
            macinout.setCreateOrderDeptId(macInOutDto.getCreateOrderDeptId());
            macinout.setDeleteFlag(0);
            //获取前端修改后新的施工人员
            List<Worker> newWorkerList = macInOutDto.getWorker();
            Worker worker = new Worker();
            String macInoutId = macInOutDto.getUuid();

            //根据macInoutId删除原有施工人员数据
            workerMapper.deleteWorkerByMacInoutId(macInoutId);
            //重新插入新施工人员数据
            if (newWorkerList.size() != 0) {
                for (int i = 0; i < newWorkerList.size(); i++) {
                    String workId = UUID.randomUUID().toString().trim().replaceAll("-", "");
                    worker.setUuid(workId);
                    worker.setMacInoutId(macInoutId);
                    worker.setConstructionDept(newWorkerList.get(i).getConstructionDept());
                    worker.setConstructionDeptId(newWorkerList.get(i).getConstructionDeptId());
                    worker.setWorkTel(newWorkerList.get(i).getWorkTel());
                    worker.setWorkIdCard(newWorkerList.get(i).getWorkIdCard());
                    worker.setDeleteFlag(0);
                    worker.setWorkerId(newWorkerList.get(i).getWorkerId());
                    worker.setWorkName(newWorkerList.get(i).getWorkName());
                    workerMapper.insert(worker);
                }
            }
            //根据macInoutId删除原有施工人员数据
            macinoutAssociationMapper.deleteMacByMacInoutId(macInoutId);
            List<MacinoutAssociation> macList = macInOutDto.getMacList();
            if (macList.size() != 0) {
                for (int i = 0; i < macList.size(); i++) {
                    MacinoutAssociation macinoutAssociation = new MacinoutAssociation();
                    String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
                    macinoutAssociation.setUuid(uuid);
                    macinoutAssociation.setDeleteFlag(0);
                    macinoutAssociation.setMacId(macList.get(i).getMacId());
                    macinoutAssociation.setMacinoutId(macInoutId);
                    macinoutAssociation.setMacName(macList.get(i).getMacName());
                    macinoutAssociation.setMacprincipal(macList.get(i).getMacprincipal());
                    macinoutAssociation.setMacprincipalId(macList.get(i).getMacprincipalId());
                    macinoutAssociation.setMacprincipalTel(macList.get(i).getMacprincipalTel());
                    macinoutAssociation.setUpdateTime(DateUtil.now());
                    macinoutAssociation.setCreateTime(DateUtil.now());
                    macinoutAssociation.setCreateBy(userName);
                    macinoutAssociation.setUpdateBy(userName);
                    macinoutAssociation.setOrderStatus(macInOutDto.getOrderStatus());
                    macinoutAssociationMapper.insert(macinoutAssociation);
                }
            }
            result = updateById(macinout);
        }else {
            List<Worker> workerList = macInOutDto.getWorker();
            //表单中的机房信息
            List<MacinoutAssociation> macList1 = macInOutDto.getMacList();
            //查询原工单中的机房信息
            QueryWrapper<MacinoutAssociation> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(MacinoutAssociation::getDeleteFlag, 0);
            queryWrapper.lambda().eq(MacinoutAssociation::getMacinoutId, macInOutDto.getUuid());
            List<MacinoutAssociation> macList2 = macinoutAssociationMapper.selectList(queryWrapper);

            Map<String, List<MacinoutAssociation>> macMap = macList1.stream().collect(Collectors.groupingBy(t -> t.getMacprincipalId()));
            for(Map.Entry<String, List<MacinoutAssociation>> entry : macMap.entrySet()){
                //获取分组后的机房信息
                List<MacinoutAssociation> macList =entry.getValue();
            if(macList.get(0).getMacprincipalId().equals(macList2.get(0).getMacprincipalId())){
                        Macinout macinout = new Macinout();
                        Worker worker = new Worker();
                        String macInoutuuid = macInOutDto.getUuid();
                        MacinoutAssociation macinoutAssociation = new MacinoutAssociation();
                        macinout.setOrderNo(macInOutDto.getOrderNo());
                        macinout.setUuid(macInoutuuid);
                        macinout.setDeleteFlag(0);
                        macinout.setOrderStatus(macInOutDto.getOrderStatus());
                        macinout.setCheckDetail("");
                        macinout.setAlarm(macInOutDto.getAlarm());
                        macinout.setCreateTime(DateUtil.now());
                        macinout.setUpdateTime(DateUtil.now());
                        macinout.setMacPrincipal(macInOutDto.getMacPrincipal());
                        macinout.setOrderId(macInOutDto.getOrderId());
                        macinout.setOrderThirdName(macInOutDto.getOrderThirdName());
                        macinout.setCabinetName(macInOutDto.getCabinetName());
                        macinout.setCabinetId(macInOutDto.getCabinetId());
                        macinout.setCity(macInOutDto.getCity());
                        macinout.setConstructionDept(macInOutDto.getConstructionDept());
                        macinout.setConstructionDeptId(macInOutDto.getConstructionDeptId());
                        macinout.setCreateOrderUserId(macInOutDto.getCreateOrderUserId());
                        //		macinout.setCreateOrderUserName(SecurityUtils.getUsername());
                        macinout.setMacName(macInOutDto.getMacName());
                        macinout.setMacId(macInOutDto.getMacId());
                        macinout.setOrderName(macInOutDto.getOrderName());
                        macinout.setOrderTheme(macInOutDto.getOrderTheme());
                        macinout.setWorkBeginTime(macInOutDto.getWorkBeginTime());
                        macinout.setWorkEndTime(macInOutDto.getWorkEndTime());
                        macinout.setOtherWork(macInOutDto.getOtherWork());
                        macinout.setWorkType(macInOutDto.getWorkType());
                        macinout.setFreeWorkerName(macInOutDto.getFreeWorkerName());
                        macinout.setWorkIngPrincpcipal(macInOutDto.getWorkIngPrincpcipal());
                        macinout.setWorkIngPrincpcipalTel(macInOutDto.getWorkIngPrincpcipalTel());
                        macinout.setWorkIngPrincpcipalDept(macInOutDto.getWorkIngPrincpcipalDept());
                        macinout.setWorkIngPrincpcipalDeptId(macInOutDto.getWorkIngPrincpcipalDeptId());
                        macinout.setWorkIngPrincpcipalId(macInOutDto.getWorkIngPrincpcipalId());
                        macinout.setFreeWorker(macInOutDto.getFreeWorker());
                        macinout.setFreeDept(macInOutDto.getFreeDept());
                        macinout.setFreeDeptId(macInOutDto.getFreeDeptId());
                        macinout.setCreateOrderUserName(macInOutDto.getCreateOrderUserName());
                        macinout.setCreateOrderDeptId(macInOutDto.getCreateOrderDeptId());
                        SysDept dept =deptService.selectDeptById(Integer.valueOf(macInOutDto.getCreateOrderDeptId()));
                        //macinout.setCreateOrderDept(macInOutDto.getCreateOrderDept());
                        macinout.setCreateOrderDept(dept.getDeptName());
                        result = updateById(macinout);

                        //记录工单审核状态
                        iMacCheckRecordService.insertCheckRecord(macInOutDto.getLoginUserId()+"",macinout.getOrderStatus()+"",macInoutuuid);

                         workerMapper.deleteWorkerByMacInoutId(macInoutuuid);
                        if (workerList.size() != 0) {
                            for (int i = 0; i < workerList.size(); i++) {
                                String workId = UUID.randomUUID().toString().trim().replaceAll("-", "");
                                worker.setUuid(workId);
                                worker.setMacInoutId(macInOutDto.getUuid());
                                worker.setConstructionDept(workerList.get(i).getConstructionDept());
                                worker.setConstructionDeptId(workerList.get(i).getConstructionDeptId());
                                worker.setWorkTel(workerList.get(i).getWorkTel());
                                worker.setWorkIdCard(workerList.get(i).getWorkIdCard());
                                worker.setDeleteFlag(0);
                                worker.setWorkerId(workerList.get(i).getWorkerId());
                                worker.setWorkName(workerList.get(i).getWorkName());
                                workerMapper.insert(worker);
                            }
                        }
                          //根据macInoutId删除原有施工人员数据
                          macinoutAssociationMapper.deleteMacByMacInoutId(macInoutuuid);
                        if (macList.size() != 0) {
                            for (int i = 0; i < macList.size(); i++) {
                                String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
                                macinoutAssociation.setUuid(uuid);
                                macinoutAssociation.setMacId(macList.get(i).getMacId());
                                macinoutAssociation.setDeleteFlag(0);
                                macinoutAssociation.setMacinoutId(macInOutDto.getUuid());
                                macinoutAssociation.setMacName(macList.get(i).getMacName());
                                macinoutAssociation.setMacprincipal(macList.get(i).getMacprincipal());
                                macinoutAssociation.setMacprincipalId(macList.get(i).getMacprincipalId());
                                macinoutAssociation.setMacprincipalTel(macList.get(i).getMacprincipalTel());
                                macinoutAssociation.setUpdateTime(DateUtil.now());
                                macinoutAssociation.setCreateTime(DateUtil.now());
                                macinoutAssociation.setCreateBy(macInOutDto.getCreateOrderUserName());
                                macinoutAssociation.setUpdateBy(macInOutDto.getCreateOrderUserName());
                                macinoutAssociation.setOrderStatus(macInOutDto.getOrderStatus());
                                macinoutAssociationMapper.insert(macinoutAssociation);

                                SysUser sysUser = sysUserMapper.selectUserById(Integer.valueOf(macinoutAssociation.getMacprincipalId()));
                                SysMessage sysMessage = new SysMessage();
                                sysMessage.setId(UUID.randomUUID().toString());
                                sysMessage.setStatus("0");
                                sysMessage.setCreateTime(new Date());
                                sysMessage.setType("2");
                                sysMessage.setUserId(sysUser.getIdCard());
                                JSONObject jsonObject = new JSONObject();
                                jsonObject.put("id", macinout.getUuid());
                                jsonObject.put("type", macinout.getWorkType());
                                sysMessage.setContent(JSONObject.toJSONString(jsonObject));
                                sysMessageMapper.insert(sysMessage);
                                JSONObject jsonObject1 = new JSONObject();
                                jsonObject1.put("vmc", sysUser.getIdCard());
                                jsonObject1.put("message", JSONObject.toJSONString(sysMessage));
                               // HttpUtils.post(webSocketPath, JSONObject.toJSONString(jsonObject1));
                            }
                        }
                    }else{
                        Macinout macinout = new Macinout();
                        String macInoutuuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
                        Worker worker = new Worker();
                        MacinoutAssociation macinoutAssociation = new MacinoutAssociation();
                        //插入割接工单号
                        if (macInOutDto.getWorkType() == 1) {
                            Calendar cal = Calendar.getInstance();
                            Date date = cal.getTime();
                            String orderNo = "NXGJ"+"-"+ new SimpleDateFormat("yyyyMMddHHmmss").format(date);
                            macinout.setOrderNo(orderNo);
                        }
                        //插入施工工单号
                        if (macInOutDto.getWorkType() == 2) {
                            Calendar cal = Calendar.getInstance();
                            Date date = cal.getTime();
                            String orderNo = "NXSG"+"-" + new SimpleDateFormat("yyyyMMddHHmmss").format(date);
                            macinout.setOrderNo(orderNo);
                        }
                        //插入安检工单号
                        if (macInOutDto.getWorkType() == 3) {
                            Calendar cal = Calendar.getInstance();
                            Date date = cal.getTime();
                            String orderNo = "NXAJ"+"-" + new SimpleDateFormat("yyyyMMddHHmmss").format(date);
                            macinout.setOrderNo(orderNo);
                        }
                        //插入其它类型工单号
                        if (macInOutDto.getWorkType() == 4) {
                            Calendar cal = Calendar.getInstance();
                            Date date = cal.getTime();
                            String orderNo = "NXQT"+"-" + new SimpleDateFormat("yyyyMMddHHmmss").format(date);
                            macinout.setOrderNo(orderNo);
                        }
                        macinout.setUuid(macInoutuuid);
                        macinout.setDeleteFlag(0);
                        macinout.setOrderStatus(macInOutDto.getOrderStatus());

                        macinout.setAlarm(macInOutDto.getAlarm());
                        macinout.setCreateTime(DateUtil.now());
                        macinout.setUpdateTime(DateUtil.now());
                        macinout.setMacPrincipal(macInOutDto.getMacPrincipal());
                        macinout.setOrderId(macInOutDto.getOrderId());
                        macinout.setOrderThirdName(macInOutDto.getOrderThirdName());
                        macinout.setCabinetName(macInOutDto.getCabinetName());
                        macinout.setCabinetId(macInOutDto.getCabinetId());
                        macinout.setCity(macInOutDto.getCity());
                        macinout.setConstructionDept(macInOutDto.getConstructionDept());
                        macinout.setConstructionDeptId(macInOutDto.getConstructionDeptId());
                        macinout.setCreateOrderUserId(macInOutDto.getCreateOrderUserId());
                        //		macinout.setCreateOrderUserName(SecurityUtils.getUsername());
                        macinout.setMacName(macInOutDto.getMacName());
                        macinout.setMacId(macInOutDto.getMacId());
                        macinout.setOrderName(macInOutDto.getOrderName());
                        macinout.setOrderTheme(macInOutDto.getOrderTheme());
                        macinout.setWorkBeginTime(macInOutDto.getWorkBeginTime());
                        macinout.setWorkEndTime(macInOutDto.getWorkEndTime());
                        macinout.setOtherWork(macInOutDto.getOtherWork());
                        macinout.setWorkType(macInOutDto.getWorkType());
                        macinout.setFreeWorkerName(macInOutDto.getFreeWorkerName());
                        macinout.setWorkIngPrincpcipal(macInOutDto.getWorkIngPrincpcipal());
                        macinout.setWorkIngPrincpcipalTel(macInOutDto.getWorkIngPrincpcipalTel());
                        macinout.setWorkIngPrincpcipalDept(macInOutDto.getWorkIngPrincpcipalDept());
                        macinout.setWorkIngPrincpcipalDeptId(macInOutDto.getWorkIngPrincpcipalDeptId());
                        macinout.setWorkIngPrincpcipalId(macInOutDto.getWorkIngPrincpcipalId());
                        macinout.setFreeWorker(macInOutDto.getFreeWorker());
                        macinout.setFreeDept(macInOutDto.getFreeDept());
                        macinout.setFreeDeptId(macInOutDto.getFreeDeptId());
                        macinout.setCreateOrderUserName(macInOutDto.getCreateOrderUserName());
                        macinout.setCreateOrderDeptId(macInOutDto.getCreateOrderDeptId());
                        SysDept dept =deptService.selectDeptById(Integer.valueOf(macInOutDto.getCreateOrderDeptId()));
                        //macinout.setCreateOrderDept(macInOutDto.getCreateOrderDept());
                        macinout.setCreateOrderDept(dept.getDeptName());
                        result = save(macinout);

                        //记录工单审核状态
                        iMacCheckRecordService.insertCheckRecord(macInOutDto.getLoginUserId()+"",macinout.getOrderStatus()+"",macInoutuuid);

                        if (workerList.size() != 0) {
                            for (int i = 0; i < workerList.size(); i++) {
                                String workId = UUID.randomUUID().toString().trim().replaceAll("-", "");
                                worker.setUuid(workId);
                                worker.setMacInoutId(macInoutuuid);
                                worker.setConstructionDept(workerList.get(i).getConstructionDept());
                                worker.setConstructionDeptId(workerList.get(i).getConstructionDeptId());
                                worker.setWorkTel(workerList.get(i).getWorkTel());
                                worker.setWorkIdCard(workerList.get(i).getWorkIdCard());
                                worker.setDeleteFlag(0);
                                worker.setWorkerId(workerList.get(i).getWorkerId());
                                worker.setWorkName(workerList.get(i).getWorkName());
                                workerMapper.insert(worker);
                            }
                        }
                        if (macList.size() != 0) {
                            for (int i = 0; i < macList.size(); i++) {
                                String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
                                macinoutAssociation.setUuid(uuid);
                                macinoutAssociation.setMacId(macList.get(i).getMacId());
                                macinoutAssociation.setDeleteFlag(0);
                                macinoutAssociation.setMacinoutId(macInoutuuid);
                                macinoutAssociation.setMacName(macList.get(i).getMacName());
                                macinoutAssociation.setMacprincipal(macList.get(i).getMacprincipal());
                                macinoutAssociation.setMacprincipalId(macList.get(i).getMacprincipalId());
                                macinoutAssociation.setMacprincipalTel(macList.get(i).getMacprincipalTel());
                                macinoutAssociation.setUpdateTime(DateUtil.now());
                                macinoutAssociation.setCreateTime(DateUtil.now());
                                macinoutAssociation.setCreateBy(macInOutDto.getCreateOrderUserName());
                                macinoutAssociation.setUpdateBy(macInOutDto.getCreateOrderUserName());
                                macinoutAssociation.setOrderStatus(macInOutDto.getOrderStatus());
                                macinoutAssociationMapper.insert(macinoutAssociation);

                                SysUser sysUser = sysUserMapper.selectUserById(Integer.valueOf(macinoutAssociation.getMacprincipalId()));
                                SysMessage sysMessage = new SysMessage();
                                sysMessage.setId(UUID.randomUUID().toString());
                                sysMessage.setStatus("0");
                                sysMessage.setCreateTime(new Date());
                                sysMessage.setType("2");
                                sysMessage.setUserId(sysUser.getIdCard());
                                JSONObject jsonObject = new JSONObject();
                                jsonObject.put("id", macinout.getUuid());
                                jsonObject.put("type", macinout.getWorkType());
                                sysMessage.setContent(JSONObject.toJSONString(jsonObject));
                                sysMessageMapper.insert(sysMessage);
                                JSONObject jsonObject1 = new JSONObject();
                                jsonObject1.put("vmc", sysUser.getIdCard());
                                jsonObject1.put("message", JSONObject.toJSONString(sysMessage));
                                HttpUtils.post(webSocketPath, JSONObject.toJSONString(jsonObject1));
                            }
                        }
                    }
                }
        }
        return result;
    }

    @Override
    public boolean deleteMacinout(String uuids) {
        String[] uuidList = uuids.split(",", -1);
        Macinout macinout = new Macinout();
        boolean result = false;
        for (String str : uuidList) {
            macinout = macinoutMapper.selectById(str);
            if (macinout.getOrderStatus() == 1) {
                macinout.setDeleteFlag(1);
                result = updateById(macinout);
            }
        }
        return result;
    }

    @Override
    public MacInOutDto getMacinoutById(String uuid) {
        MacInOutDto macInOutDto = new MacInOutDto();
        Macinout macinout = new Macinout();
        List<Worker> worker = new ArrayList<>();
        macinout = macinoutMapper.selectById(uuid);
        macInOutDto.setUuid(macinout.getUuid());
        macInOutDto.setOrderStatus(macinout.getOrderStatus());
        macInOutDto.setCreateTime(macinout.getCreateTime());
        macInOutDto.setUpdateTime(macinout.getUpdateTime());
        macInOutDto.setMacPrincipal(macinout.getMacPrincipal());
        macInOutDto.setCabinetName(macinout.getCabinetName());
        macInOutDto.setCabinetId(macinout.getCabinetId());
        macInOutDto.setCity(macinout.getCity());
        macInOutDto.setConstructionDept(macinout.getConstructionDept());
        macInOutDto.setConstructionDeptId(macinout.getConstructionDeptId());
        macInOutDto.setCreateOrderUserId(macinout.getCreateOrderUserId());
//		macinout.setCreateOrderUserName(SecurityUtils.getUsername());
        macInOutDto.setMacName(macinout.getMacName());
        macInOutDto.setMacId(macinout.getMacId());
        macInOutDto.setOrderName(macinout.getOrderName());
        macInOutDto.setOrderTheme(macinout.getOrderTheme());
        macInOutDto.setWorkBeginTime(macinout.getWorkBeginTime());
        macInOutDto.setWorkEndTime(macinout.getWorkEndTime());
        macInOutDto.setOtherWork(macinout.getOtherWork());
        macInOutDto.setWorkType(macinout.getWorkType());
        macInOutDto.setWorkIngPrincpcipal(macinout.getWorkIngPrincpcipal());
        macInOutDto.setWorkIngPrincpcipalTel(macinout.getWorkIngPrincpcipalTel());
        macInOutDto.setWorkIngPrincpcipalId(macinout.getWorkIngPrincpcipalId());
        macInOutDto.setWorkIngPrincpcipalDept(macinout.getWorkIngPrincpcipalDept());
        macInOutDto.setWorkIngPrincpcipalDeptId(macinout.getWorkIngPrincpcipalDeptId());
        macInOutDto.setFreeWorkerName(macinout.getFreeWorkerName());
        macInOutDto.setFreeWorker(macinout.getFreeWorker());
        macInOutDto.setFreeDept(macinout.getFreeDept());
        macInOutDto.setFreeDeptId(macinout.getFreeDeptId());
        macInOutDto.setCreateOrderUserName(macinout.getCreateOrderUserName());
        macInOutDto.setCreateOrderDept(macinout.getCreateOrderDept());
        macInOutDto.setCreateOrderDeptId(macinout.getCreateOrderDeptId());
        macInOutDto.setOrderId(macinout.getOrderId());
        macInOutDto.setCheckDetail(macinout.getCheckDetail());
        macInOutDto.setAlarm(macinout.getAlarm());
        macInOutDto.setOrderNo(macinout.getOrderNo());
//		worker = workerMapper.getWorkByMacInoutId(uuid);
//		macInOutDto.setWorker(worker);
        return macInOutDto;
    }

    @Override
    public List<String> getWorkderIds(String macInoutId) {
        List<String> workIds = workerMapper.getWorkByMacInoutId(macInoutId);
        return workIds;
    }

    @Override
    public List<Worker> getWorkerList(String macInoutId) {
        QueryWrapper<Worker> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Worker::getDeleteFlag, 0);
        queryWrapper.lambda().eq(Worker::getMacInoutId, macInoutId);
        return workerMapper.selectList(queryWrapper);
    }

    @Override
    public List<MacinoutAssociation> getMacList(String macInoutId) {
//        Integer userId = tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId();
        QueryWrapper<MacinoutAssociation> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MacinoutAssociation::getDeleteFlag, 0);
        queryWrapper.lambda().eq(MacinoutAssociation::getMacinoutId, macInoutId);
//        queryWrapper.lambda().eq(MacinoutAssociation::getMacprincipalId, userId);
        return macinoutAssociationMapper.selectList(queryWrapper);
    }

    @Override
    public MacInOutDto getCheckDetail(String macInoutId) {
        MacInOutDto macInOutDto = new MacInOutDto();
        Macinout macinout = new Macinout();
        List<Worker> worker = new ArrayList<>();
        macinout = macinoutMapper.selectById(macInoutId);
        macInOutDto.setUuid(macinout.getUuid());
        macInOutDto.setOrderStatus(macinout.getOrderStatus());
        macInOutDto.setCreateTime(macinout.getCreateTime());
        macInOutDto.setUpdateTime(macinout.getUpdateTime());
        macInOutDto.setMacPrincipal(macinout.getMacPrincipal());
        macInOutDto.setCabinetName(macinout.getCabinetName());
        macInOutDto.setCabinetId(macinout.getCabinetId());
        macInOutDto.setCity(macinout.getCity());
        macInOutDto.setConstructionDept(macinout.getConstructionDept());
        macInOutDto.setConstructionDeptId(macinout.getConstructionDeptId());
        macInOutDto.setCreateOrderUserId(macinout.getCreateOrderUserId());
//		macinout.setCreateOrderUserName(SecurityUtils.getUsername());
        macInOutDto.setMacName(macinout.getMacName());
        macInOutDto.setMacId(macinout.getMacId());
        macInOutDto.setOrderName(macinout.getOrderName());
        macInOutDto.setOrderTheme(macinout.getOrderTheme());
        macInOutDto.setWorkBeginTime(macinout.getWorkBeginTime());
        macInOutDto.setWorkEndTime(macinout.getWorkEndTime());
        macInOutDto.setOtherWork(macinout.getOtherWork());
        macInOutDto.setWorkType(macinout.getWorkType());
        macInOutDto.setWorkIngPrincpcipal(macinout.getWorkIngPrincpcipal());
        macInOutDto.setWorkIngPrincpcipalTel(macinout.getWorkIngPrincpcipalTel());
        macInOutDto.setWorkIngPrincpcipalId(macinout.getWorkIngPrincpcipalId());
        macInOutDto.setFreeWorker(macinout.getFreeWorker());
        macInOutDto.setFreeDept(macinout.getFreeDept());
        macInOutDto.setFreeDeptId(macinout.getFreeDeptId());
        macInOutDto.setCreateOrderUserName(macinout.getCreateOrderUserName());
        macInOutDto.setCreateOrderDept(macinout.getCreateOrderDept());
        macInOutDto.setCreateOrderDeptId(macinout.getCreateOrderDeptId());
        macInOutDto.setCheckDetail(macinout.getCheckDetail());
        worker = workerMapper.getWorkListByMacInoutId(macInoutId);
        macInOutDto.setWorker(worker);
        return macInOutDto;
    }

    @Override
    public int checkMacInout(Macinout macInout) {
        Macinout detail = new Macinout();
        detail = macinoutMapper.selectById(macInout.getUuid());
        detail.setOrderStatus(macInout.getOrderStatus());
        if (StringUtils.isNotBlank(macInout.getCheckDetail())) {
            detail.setCheckDetail(macInout.getCheckDetail());
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 修改入库状态

        String name = loginUser.getUser().getNickname();
        String now = DateUtil.now();
        detail.setApproveBy(name);
        detail.setApproveTime(now);
        //查询建单下所有施工机房
        QueryWrapper<MacinoutAssociation> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MacinoutAssociation::getDeleteFlag, 0);
        queryWrapper.lambda().eq(MacinoutAssociation::getMacinoutId, detail.getUuid());
        //queryWrapper.lambda().eq(MacinoutAssociation::getMacprincipalId, SecurityUtils.getLoginUser().getUser().getUserId());
        List<MacinoutAssociation> macList = macinoutAssociationMapper.selectList(queryWrapper);

        //记录工单审核状态
        iMacCheckRecordService.insertCheckRecord(macInout.getLoginUserId()+"",macInout.getOrderStatus()+"",macInout.getUuid());

        //修改当前审核人下所有施工机房建单状态
        if (macList.size() > 0) {
            for (int i = 0; i < macList.size(); i++) {
                macList.get(i).setOrderStatus(macInout.getOrderStatus());
                macinoutAssociationMapper.updateById(macList.get(i));
                //如果当前审核人下机房建单状体内有一条审核状态为驳回，则此订单驳回
            }
        }
        return macinoutMapper.updateById(detail);
    }

    @Override
    public SysDept getDeptByUserId(String userId) {
        SysUser sysUser = new SysUser();
        SysDept sysDept = new SysDept();
        sysUser = sysUserMapper.selectById(userId);
        sysDept = sysDeptMapper.selectById(sysUser.getDeptId());
        return sysDept;
    }

    @Override
    public List<SysUser> getOwnner(String deptId) {
        return sysUserMapper.selectOwnUserList(deptId);
    }

    @Override
    public List<SysDept> treeselectFreeWorkerDept(SysDept sysDept) {
        return sysDeptMapper.selectOwnDeptList();
    }
    @Override
    public List<SysDept> owerThirdTreeList(SysDept sysDept) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        return sysDeptMapper.selectThirdDeptList(user.getDeptId());
    }
    @Override
    public SysDept getCurrentUserDept() {
        SysUser sysUser = new SysUser();
        sysUser = sysUserMapper.selectById(SecurityUtils.getLoginUser().getUser().getUserId());
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysDept::getDelFlag, 0);
        queryWrapper.lambda().eq(SysDept::getDeptId, sysUser.getDeptId());
        return sysDeptMapper.selectOne(queryWrapper);
    }

    @Override
    public SysUser getCurrentUser() {
        return sysUserMapper.selectById(SecurityUtils.getLoginUser().getUser().getUserId());
    }

    @Override
    public String uploadFile(MultipartFile file) throws IOException {
        // 图片存储路径
        String path = "D:\\fileSave";
        // 判断是否有路径无则生成路径
        if (!new File(path).exists()) {
            new File(path).mkdirs();
        }
        //创建文件名
        String fileName = UUID.randomUUID().toString().replace("-", "") + ".jpg";
        //以下为图片本地存储方式
        File tempFile = new File(path, fileName);
        //若文件不存在创建新文件
        if (!tempFile.exists()) {
            tempFile.createNewFile();
        }
        //将内存中图片写入磁盘
        file.transferTo(tempFile);
        return tempFile.toString();
    }

    @Override
    public List<SysUser> selectWorkIngPrincpcipal(String workIngPrincpcipal) {
        return sysUserMapper.selectWorkIngPrincpcipal(workIngPrincpcipal);
    }

    @Override
    public List<MacRoom> selectMacRoom(String macName) {
        return macRoomMapper.selectMacRoom(macName);
    }

    @Override
    public List<SysUser> getWorker(String deptId) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUser::getDelFlag, 0);
        queryWrapper.lambda().eq(SysUser::getDeptId, deptId);
        return sysUserMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<Macinout> findMacRoomByNameForPageApp(Macinout macinout) {
        QueryWrapper<Macinout> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Macinout::getDeleteFlag, 0);
        queryWrapper.lambda().eq(Macinout::getCreateOrderUserId, macinout.getCreateOrderUserId());
        queryWrapper.lambda().orderByDesc(Macinout::getUpdateTime);
        List<Integer> worktypes=new ArrayList<>();
        for(String workdtype:macinout.getQueryWorkType().split(",")) {
            worktypes.add(Integer.valueOf(workdtype));
        }
        List<Integer> orderStatus=new ArrayList<>();
        for(String orderStatu:macinout.getQueryOrderStatus().split(",")) {
            orderStatus.add(Integer.valueOf(orderStatu));
        }
        queryWrapper.lambda().in(Macinout::getWorkType, worktypes);
        if(!orderStatus.isEmpty()){
            queryWrapper.lambda().in(Macinout::getOrderStatus, orderStatus);
        }
        if (macinout != null) {
            if (StrUtil.isNotBlank(macinout.getOrderName())) {
                queryWrapper.lambda().like(Macinout::getOrderName, macinout.getOrderName());
            }
        }
        return page(new Page<>(macinout.getPageNum(), macinout.getPageSize()), queryWrapper);
    }

    @Override
    public boolean approveMacinoutByApp(Macinout macInout) {

        Macinout detail = new Macinout();
        SysUser user = sysUserMapper.selectUserById(Integer.valueOf(macInout.getLoginUserId()));
        detail = macinoutMapper.selectById(macInout.getUuid());
        detail.setOrderStatus(macInout.getOrderStatus());
        if (StringUtils.isNotBlank(macInout.getCheckDetail())) {
            detail.setCheckDetail(macInout.getCheckDetail());
        }
        //查询建单下所有施工机房
        QueryWrapper<MacinoutAssociation> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MacinoutAssociation::getDeleteFlag, 0);
        queryWrapper.lambda().eq(MacinoutAssociation::getMacinoutId, detail.getUuid());
        List<MacinoutAssociation> macList = macinoutAssociationMapper.selectList(queryWrapper);


        //修改当前审核人下所有施工机房建单状态
        if (macList.size() > 0) {
            for (int i = 0; i < macList.size(); i++) {
                macList.get(i).setOrderStatus(macInout.getOrderStatus());
                macinoutAssociationMapper.updateById(macList.get(i));
                //如果当前审核人下机房建单状体内有一条审核状态为驳回，则此订单驳回
            }
        }
        //记录工单审核状态
        iMacCheckRecordService.insertCheckRecord(macInout.getLoginUserId()+"",macInout.getOrderStatus()+"",macInout.getUuid());

        macinoutMapper.updateById(detail);
        SysUser sysUser = sysUserMapper.selectUserById(Integer.valueOf(macInout.getCreateOrderUserId()));
        SysMessage sysMessage = new SysMessage();
        sysMessage.setId(UUID.randomUUID().toString());
        sysMessage.setStatus("0");
        sysMessage.setCreateTime(new Date());
        sysMessage.setType("1");
        sysMessage.setUserId(sysUser.getIdCard());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", macInout.getUuid());
        jsonObject.put("type", macInout.getWorkType());
        sysMessage.setContent(JSONObject.toJSONString(jsonObject));
        sysMessageMapper.insert(sysMessage);
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("vmc", sysUser.getIdCard());
        jsonObject1.put("message", JSONObject.toJSONString(sysMessage));
        HttpUtils.post(webSocketPath, JSONObject.toJSONString(jsonObject1));
        return true;
    }

    @Override
    public IPage<Map<String, Object>> getWaitAcceptList(String queryOrderStatus,String userid, int pageNum, int pageSize) {
        return macinoutMapper.getWaitCheckList(new Page<>(pageNum, pageSize), userid,queryOrderStatus);
    }
    @Override
    public IPage<Map<String, Object>> getWaitAcceptListWeb(String queryOrderStatus,String userid, int pageNum, int pageSize) {
        return macinoutMapper.getWaitCheckListWeb(new Page<>(pageNum, pageSize), userid, queryOrderStatus);
    }
    /**
     * 查询导出列表
     *
     * @param macinout 查询条件
     * @return 结果
     */
    @Override
    public List<Macinout> selectExportList(Macinout macinout) {
        SysUser sysUser = sysUserMapper.selectById(SecurityUtils.getLoginUser().getUser().getUserId());
        macinout.setCreateOrderUserName(sysUser.getNickname());
        QueryWrapper<Macinout> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Macinout::getDeleteFlag, 0)
                .eq(Macinout::getCreateOrderUserName, macinout.getCreateOrderUserName())
                .orderByDesc(Macinout::getUpdateTime);
        if (macinout != null) {
            if (StrUtil.isNotBlank(macinout.getOrderName())) {
                queryWrapper.lambda().like(Macinout::getOrderName, macinout.getOrderName());
            }
            if (StrUtil.isNotBlank(macinout.getCreateOrderDeptId())) {
                queryWrapper.lambda().eq(Macinout::getCreateOrderDeptId, macinout.getCreateOrderDeptId());
            }
            if (macinout.getWorkType() != null) {
                queryWrapper.lambda().eq(Macinout::getWorkType, macinout.getWorkType());
            }
        }
        List<Macinout> list = list(queryWrapper);
        for (Macinout inout : list) {
            if (inout.getWorkType() == 1) {
                inout.setWorkTypeStr("割接");
            } else if (inout.getWorkType() == 2) {
                inout.setWorkTypeStr("施工");
            } else if (inout.getWorkType() == 3) {
                inout.setWorkTypeStr("安全检查");
            } else if (inout.getWorkType() == 4) {
                inout.setWorkTypeStr("其他");
            }

            if (inout.getOrderStatus() == 1) {
                inout.setOrderStatusStr("待提交");
            } else if (inout.getOrderStatus() == 2) {
                inout.setOrderStatusStr("待审核");
            } else if (inout.getOrderStatus() == 3) {
                inout.setOrderStatusStr("已驳回");
            } else if (inout.getOrderStatus() == 4) {
                inout.setOrderStatusStr("待验收");
            } else if (inout.getOrderStatus() == 5) {
                inout.setOrderStatusStr("已验收");
            } else if (inout.getOrderStatus() == 6) {
                inout.setOrderStatusStr("已终止");
            }
        }
        return list;
    }

}
